var pageCount = 50;
var currentPage;

var statusesLoadedIds = [];
var statusesSelectedIds = [];

$(document).ready(function() {
	currentPage = 1;

    $('#statusesLoadingDiv').modal({
    	backdrop : false,
    	keyboard : false,
    	show : false
    });

    $('#statusesDeletingDiv').modal({
    	backdrop : false,
    	keyboard : false,
    	show : false
    });
	
	getStatuses();

	$(window).scrollTop(0);
});

function getStatuses() {
	$.ajax({
		async: true, 
		type: 'GET', 
		dataType: 'json', 
		url: 'action.php', 
		data: {'action' : 'queryStatuses', 'userId' : uid, 'page' : currentPage, 'count' : pageCount}, 
		beforeSend: function() {
			$('#statusesLoadingDiv').modal('show');
		},
		success: function(data, textStatus) {
			$('#statusesLoadingDiv').modal('hide');
			
			var totalNumber = data['totalNumber'];
			var statuses = data['statuses'];

			var totalPage = Math.ceil(totalNumber / pageCount);
			
			if (currentPage > totalPage) {
				if (totalPage > 0) {
					currentPage = totalPage;

					getStatuses();
				}
			} else {
				$('#statusesCheckbox').prop('checked', false);

				$('#statusesDeleteButton').attr('class', 'btn disabled').attr('disabled', 'disabled');
				
				showStatuses(statuses);

				showPaginations(totalPage);
			}
		}
	});
}

function getStatusCheckboxId(statusId) {
	return statusId + '_checkbox';
}

function getStatusDivId(statusId) {
	return statusId + '_div';
}

function addStatusIdToArray(statusesIds, statusId) {
	statusesIds.push(statusId);
}

function removeStatusIdFromArray(statusesIds, statusId) {
	var index = -1;
	
	for (var i = 0; i < statusesIds.length; i++) {
		if (statusId == statusesIds[i]) {
			index = i;
			break;
		}
	}

	if (index != -1) {
		statusesIds.splice(index, 1);
	}
}

function getArrayFromStr(data) {
	var array = [];
	
	var strArray = data.split(',');

	for (var i = 0; i < strArray.length; i++) {
		var str = strArray[i];
		
		if (str != '') {
			array.push(str);
		}
	}

	return array;
}

function showStatuses(statuses) {
	$('#statusesDiv').html('');
	
	statusesLoadedIds = [];
	statusesSelectedIds = [];
	
	for (var i = 0; i < statuses.length; i++) {
		var status = statuses[i];

		var id = status['id'];
		var text = status['text'];
		var thumbnailPic = status['thumbnailPic'];

		var statusCheckboxId = getStatusCheckboxId(id);
		var statusDivId = getStatusDivId(id);

		var thumbnailPicHtml = '';

		if (thumbnailPic != undefined && thumbnailPic != '') {
			thumbnailPicHtml = '<p>' + '<img src=\'' + thumbnailPic + '\' />' + '</p>';
		}

		var statusHtml = 
			'<div id=\'' + statusDivId + '\' class=\'row\' style=\'padding-top: 10px; border-bottom: 1px solid #DDDDDD;\'>'  + 
				'<div class=\'span1\'>' + 
					'<p style=\'text-align: center;\'>' + 
						'<input id=\'' + statusCheckboxId + '\' type=\'checkbox\' onclick=\'clickStatusCheckbox(' + id + ')\'/>' + 
					'</p>' + 
				'</div>' + 
				'<div class=\'span9\'>' + 
					'<p>' + 
						text + 
					'</p>' + 
					thumbnailPicHtml + 
				'</div>' + 
				'<div class=\'span2\'>' + 
					'<p style=\'text-align: center; \'>' + 
						'<button class=\'btn btn-primary\' onclick=\'clickStatusDeleteButton(' + id + ')\'>删除</button>' + 
					'</p>' + 
				'</div>' + 
			'</div>'

		$('#statusesDiv').append(statusHtml);

		addStatusIdToArray(statusesLoadedIds, id);
	}
}

function checkStatus(statusId) {
	var statusDivId = getStatusDivId(statusId);
	
	$('#' + statusDivId).css('background', '#D9EDF7');

	addStatusIdToArray(statusesSelectedIds, statusId);

	$('#statusesDeleteButton').attr('class', 'btn btn-primary').removeAttr('disabled');

	if (statusesSelectedIds.length == statusesLoadedIds.length) {
		$('#statusesCheckbox').prop('checked', true);
	}
}

function uncheckStatus(statusId) {
	var statusDivId = getStatusDivId(statusId);
	
	$('#' + statusDivId).css('background', '#FFFFFF');

	removeStatusIdFromArray(statusesSelectedIds, statusId);

	if (statusesSelectedIds.length == 0) {
		$('#statusesDeleteButton').attr('class', 'btn disabled').attr('disabled', 'disabled');
	}

	if (statusesSelectedIds.length != statusesLoadedIds.length) {
		$('#statusesCheckbox').prop('checked', false);
	}
}

function clickStatusCheckbox(statusId) {
	var statusCheckboxId = getStatusCheckboxId(statusId);

	var statusCheckboxChecked = $('#' + statusCheckboxId).prop('checked');
	
	if (statusCheckboxChecked) {
		checkStatus(statusId);
	} else {
		uncheckStatus(statusId);
	}
}

function clickStatusesCheckbox(statusesCheckboxChecked) {
	if (statusesCheckboxChecked) {
		for (var i = 0; i < statusesLoadedIds.length; i++) {
			var statusId = statusesLoadedIds[i];

			var statusCheckboxId = getStatusCheckboxId(statusId);

			$('#' + statusCheckboxId).prop('checked', true);
			
			checkStatus(statusId);
		}
	} else {
		for (var i = 0; i < statusesLoadedIds.length; i++) {
			var statusId = statusesLoadedIds[i];

			var statusCheckboxId = getStatusCheckboxId(statusId);

			$('#' + statusCheckboxId).prop('checked', false);
			
			uncheckStatus(statusId);
		}
	}
}

function deleteStatus(statusesIds) {
	$.ajax({
		async: true, 
		type: 'POST', 
		dataType: 'text', 
		url: 'action.php', 
		data: {'action' : 'deleteStatuses', 'statusesIds' : statusesIds}, 
		beforeSend: function() {
			$('#statusesDeletingDiv').modal('show');
		},
		success: function(data, textStatus) {
			$('#statusesDeletingDiv').modal('hide');
			
			var deletedStatusesIds = getArrayFromStr(data);

			for (var i = 0; i < deletedStatusesIds.length; i++) {
				var statusId = deletedStatusesIds[i];
				
				uncheckStatus(statusId);
				
				var statusDivId = getStatusDivId(statusId);

				$('#' + statusDivId).remove();

				removeStatusIdFromArray(statusesLoadedIds, statusId);

				if (statusesLoadedIds.length == 0) {
					$('#paginationDiv ul').html('');
					
					getStatuses();
				}
			}
		}
	});
}

function clickStatusDeleteButton(statusId) {
	deleteStatus(statusId);
}

function clickStatusesDeleteButton() {
	deleteStatus(statusesSelectedIds.join(','));
}

function clickPagination(page) {
	currentPage = page;

	getStatuses();
}

function showPaginations(totalPage) {
	$('#paginationDiv ul').html('');

	if (currentPage == 1) {
		$('#paginationDiv ul').append('<li href=\'#\' class=\'disabled\'><a>&lt;&lt;</a></li>');
	} else {
		$('#paginationDiv ul').append('<li><a href=\'#\' onclick=\'clickPagination(' + (currentPage - 1) + ')\'>&lt;&lt;</a></li>');
	}

	for (var i = 1; i <= totalPage; i++) {
		if (currentPage == i) {
			$('#paginationDiv ul').append('<li href=\'#\' class=\'active\'><a>' + i + '</a></li>');
		} else {
			$('#paginationDiv ul').append('<li><a href=\'#\' onclick=\'clickPagination(' + i + ')\'>' + i + '</a></li>');
		}
	}

	if (currentPage == totalPage) {
		$('#paginationDiv ul').append('<li href=\'#\' class=\'disabled\'><a>&gt;&gt;</a></li>');
	} else {
		$('#paginationDiv ul').append('<li><a href=\'#\' onclick=\'clickPagination(' + (currentPage + 1) + ')\'>&gt;&gt;</a></li>');
	}
}